package com.myapp.ruyixing.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.model.MyScoreList;
import com.myapp.ruyixing.model.MyScoreList.Score;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.LoginHelper;

public class ScoreActivity extends Activity implements OnItemClickListener {

	private ArrayList<Score> mScoreList;
	private LinearLayout mContentLayout;

	private int mScore;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			try {
				MyScoreList list = (MyScoreList) o;
				onLoadingDataFinished(true, list);
			} catch (Exception e) {
				onLoadingDataFinished(false, null);
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			onLoadingDataFinished(false, null);
		}
	};

	private void showToast(String msg) {
		Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
	}

	private void onLoadingDataStarted(String bduss, int pn, int rn) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(true);
		RYHttpClient.getMyScoreList(this, mHttpResponseHandler, bduss, pn, rn);
	}

	private void onLoadingDataFinished(boolean succeed, MyScoreList list) {
		((TitleBar) findViewById(R.id.title_bar)).showLoading(false);
		if (succeed && list != null) {
			mScore = list.myscore;
			((TextView) findViewById(R.id.text_score)).setText(mScore + "");
			if (list.items != null && list.items.size() > 0) {
				mScoreList.clear();
				mScoreList.addAll(list.items);
				loadViews();
			} else {
				showToast("您无积分数据！");
			}
		} else {
			showToast("加载积分列表失败！");
		}
	}

	private void loadViews() {
		if (mScoreList == null || mScoreList.size() == 0) {
			return;
		}

		for (int i = 0; i < mScoreList.size(); i++) {
			Score item = mScoreList.get(i);
			if (item != null) {
				LinearLayout itemView = (LinearLayout) View.inflate(this,
						R.layout.view_score_item, null);
				((TextView) itemView.findViewById(R.id.text_time))
						.setText(item.datetime);
				((TextView) itemView.findViewById(R.id.text_score))
						.setText(item.score.startsWith("-") ? item.score
								: ("+" + item.score));
				((TextView) itemView.findViewById(R.id.text_event))
						.setText(item.event);
				mContentLayout.addView(itemView);
			}
		}
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_score);
		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		mContentLayout = (LinearLayout) findViewById(R.id.layout_content);
		findViewById(R.id.text_mall).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ScoreActivity.this,
						ScoreMallActivity.class);
				startActivity(intent);
			}
		});
		mScoreList = new ArrayList<Score>();
		onLoadingDataStarted(LoginHelper.getInstance().getBDUSS(), 0, 50);
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long id) {
	}

}
